package com.indusfo.spc.pda.service.impl;

import com.indusfo.spc.pda.mapper.OrderDosingDetailMapper;
import com.indusfo.spc.pda.mapper.OrderDosingMapper;
import com.indusfo.spc.pda.pojo.OrderDosing;
import com.indusfo.spc.pda.pojo.OrderDosingDetail;
import com.indusfo.spc.pda.pojo.OrderDosingDto;
import com.indusfo.spc.pda.service.OrderDosingDetailService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @ClassName OrderDosingDetailServiceImpl
 * @Description
 * @Author louk
 * @Date 2019/12/25 16:09
 */
@Service
public class OrderDosingDetailServiceImpl implements OrderDosingDetailService {

    private static final Logger logger= LoggerFactory.getLogger(OrderDosingDetailServiceImpl.class);

    @Resource
    OrderDosingDetailMapper orderDosingDetailMapper;
    @Resource
    OrderDosingMapper orderDosingMapper;
    @Override
    public JSONObject insertOrderDosingDetail(OrderDosingDto orderDosingDto) {
        //任务单id
        Integer orderId = orderDosingDto.getOrderId();
        //物料id
        Integer matId = orderDosingDto.getMatId();
        //叫料数量
        String dosingNum = orderDosingDto.getDosingNum();
        if(orderId==null || "".equals(orderId)){
            return JSONObject.build(JSONObject.ERROR,"未获取到相关任务单信息");
        }
        if(matId==null || "".equals(matId)){
            return JSONObject.build(JSONObject.ERROR,"未获取到相关物料信息");
        }
        if(StringUtils.isEmpty(dosingNum)){
            return JSONObject.build(JSONObject.ERROR,"未获取到配料数量");
        }
        if(StringUtils.isNumeric(dosingNum)){
            if(Integer.parseInt(dosingNum)==0 || "".equals(dosingNum)){
                return JSONObject.build(JSONObject.ERROR,"配料数量必须为整数且不能为0");
            }
        }else{
            return JSONObject.build(JSONObject.ERROR,"配料数量必须为整数");
        }
        //任务单id+物料id来获取要进行新增任务单叫料信息
        OrderDosing orderDosing=new OrderDosing();
        orderDosing.setOrderId(orderId);
        Map<String, OrderDosing> orderDosingMap = orderDosingMapper.queryOrderDosingByOrderId(orderDosing);
        if(!orderDosingMap.containsKey(matId)){
            return JSONObject.build(JSONObject.ERROR,"没有该物料的相关配料信息");
        }
        OrderDosing trueOrderDosing = orderDosingMap.get(matId);
        if(trueOrderDosing.getDosingNum()!=null && Integer.parseInt(trueOrderDosing.getSumNum())-trueOrderDosing.getDosingNum()<=0){
            return JSONObject.build(JSONObject.ERROR,"当前叫料完成配送无需进行配送");
        }
        if(trueOrderDosing.getDosingNum()!=null && Integer.parseInt(dosingNum)>(Integer.parseInt(trueOrderDosing.getSumNum())-trueOrderDosing.getDosingNum())){
            return JSONObject.build(JSONObject.ERROR,"配送数量大于目前需求数量");
        }
        if(trueOrderDosing.getDosingNum()==null && Integer.parseInt(trueOrderDosing.getSumNum())< Integer.parseInt(dosingNum)){
            return JSONObject.build(JSONObject.ERROR,"配送数量大于目前需求数量");
        }
        OrderDosingDetail orderDosingDetail=new OrderDosingDetail();
        Integer orderDosingId = orderDosingMap.get(matId).getOrderDosingId();
        orderDosingDetail.setOrderDosingId(orderDosingId);
        orderDosingDetail.setDosingNum(dosingNum);
        int insert = orderDosingDetailMapper.insert(orderDosingDetail);
        if(insert>0){
            orderDosingMapper.updateDosingState(orderDosingDetail.getOrderDosingId());
            return JSONObject.oK("配料成功");
        }
        return JSONObject.build(JSONObject.ERROR,"配料失败");
    }

    public String checkParam(OrderDosingDetail orderDosingDetail){
        //任务单叫料id
        Integer orderDosingId = orderDosingDetail.getOrderDosingId();
        //配送数量
        String dosingNum = orderDosingDetail.getDosingNum();
        if(orderDosingId==null || "".equals(orderDosingId)){
            return "未获取到任务单叫料信息";
        }
        if(StringUtils.isEmpty(dosingNum)){
            return "未获取到配送数量";
        }
        if(StringUtils.isNumeric(dosingNum)){
            if(Integer.parseInt(dosingNum)==0 || "".equals(Integer.parseInt(dosingNum))){
                return "叫料数量必须为整数并且不能为0";
            }
        }else{
            return "叫料数量必须为整数";
        }
        return null;
    }
}
